package com.xxl.sso.server.service.impl;

import com.icom.sso.mapper.SysUserMapper;
import com.icom.sso.util.EncryptUtil;
import com.icom.sso.util.RetKit;
import com.xxl.sso.core.entity.PageData;
import com.xxl.sso.server.core.model.SysUser;
import com.xxl.sso.server.core.model.UserInfo;
import com.xxl.sso.server.core.result.ReturnT;
import com.xxl.sso.server.service.SysUserService;
import com.xxl.sso.server.service.UserService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private SysUserMapper sysUSerMapper;

	@Override
	public ReturnT<SysUser> findUser(String username, String password) {

		if (username == null || username.trim().length() == 0) {
			return new ReturnT<SysUser>(ReturnT.FAIL_CODE, "Please input username.");
		}
		if (password == null || password.trim().length() == 0) {
			return new ReturnT<SysUser>(ReturnT.FAIL_CODE, "Please input password.");
		}

		SysUser user = sysUSerMapper.findUserByLoginId(username);

		if (user == null) {
			return new ReturnT<SysUser>(ReturnT.FAIL_CODE, "username or password is invalid.");
		}

		if (user.getLoginId().equals(username) && user.getPassword().equals(EncryptUtil.getSM32(password))) {
			return new ReturnT<SysUser>(user);
		}

		return new ReturnT<SysUser>(ReturnT.FAIL_CODE, "username or password is invalid.");
	}

	@Override
	public PageData<SysUser> pageFindUser(Integer page, Integer rows, String deptId, String name, String login) {
		// TODO Auto-generated method stub
		List<SysUser> listAll = sysUSerMapper.findUsers(deptId, name, login);

		// 从第几条数据开始
		int firstIndex = (page - 1) * rows;
		// 到第几条数据结束
		int lastIndex = page * rows;
		// 截取查询页的数据
		List<SysUser> list = new ArrayList<>();
		if (lastIndex <= listAll.size()) {
			list = listAll.subList(firstIndex, lastIndex);
		} else {
			list = listAll.subList(firstIndex, listAll.size());
		}

		return new PageData<SysUser>(list, page, rows, listAll.size());
	}

	/**
	 * 保存修改用户密码
	 */
	public RetKit<String> editUserPassword(String loginId, String oldPassword, String password) {
		RetKit<String> ret = new RetKit<String>(null);

		SysUser user = sysUSerMapper.findUserByLoginId(loginId);

		if (user == null) {
			ret.setFail();
			ret.setMsg("服务器错误，用户未找到！");
			return ret;
		}

		if (!EncryptUtil.getSM32(oldPassword).equals(user.getPassword())) {
			ret.setFail();
			ret.setMsg("原密码错误，修改失败！");
			return ret;
		}
		// 对密码进行加密处理，使用md5 加密
		// EncryptUtil.getMd5(password);
		try {
			int rows = sysUSerMapper.editUser(loginId, EncryptUtil.getSM32(password));

			return ret;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ret.setFail();
			ret.setMsg(e.toString());
			ret.setData(e.toString());
			return ret;
		}
	}

	@Override
	public SysUser findUserByLoginId(String loginId) {
		// TODO Auto-generated method stub
		SysUser sysUser = sysUSerMapper.findUserByLoginId(loginId);
		return sysUser;
	}

	@Override
	public RetKit<String> saveUser(SysUser user) {
		RetKit<String> ret = new RetKit<String>(null);

		SysUser sysUser = sysUSerMapper.findUserByLoginId(user.getLoginId());

		if (sysUser != null) {
			ret.setFail();
			ret.setMsg("登录名已存在！");
			return ret;
		}

		// TODO Auto-generated method stub
		try {
			String pwd = user.getPassword();
			user.setPassword(EncryptUtil.getSM32(pwd));
			int row = sysUSerMapper.insertUser(user);

			return ret;
		} catch (Exception e) {
			// TODO: handle exception
			ret.setFail();
			ret.setData(e.toString());
			ret.setMsg(e.toString());
			return ret;
		}

	}

	@Override
	public SysUser findUserById(String id) {
		// TODO Auto-generated method stub

		SysUser user = sysUSerMapper.findUserById(id);
		return user;
	}

	@Override
	public RetKit<String> delUser(String id) {
		RetKit<String> ret = new RetKit<String>(null);

		try {
			SysUser user = sysUSerMapper.findUserById(id);
			if (user == null) {
				ret.setFail();
				ret.setMsg("用户不存在！");
				return ret;
			}

			int rows = sysUSerMapper.delUser(id);

			return ret;
		} catch (Exception e) {
			// TODO: handle exception
			ret.setFail();
			ret.setData(e.toString());
			ret.setMsg(e.toString());
			return ret;
		}
	}

	@Override
	public RetKit<String> resetPwd(String id) {
		RetKit<String> ret = new RetKit<String>(null);

		try {
			SysUser user = sysUSerMapper.findUserById(id);
			if (user == null) {
				ret.setFail();
				ret.setMsg("用户不存在！");
				return ret;
			}

			int rows = sysUSerMapper.resetUserPwd(id,EncryptUtil.getSM32("000000"));
			return ret;
		} catch (Exception e) {
			// TODO: handle exception
			ret.setFail();
			ret.setData(e.toString());
			ret.setMsg(e.toString());
			return ret;
		}
	}

	@Override
	public RetKit<String> saveEditInfo(SysUser user) {
		RetKit<String> ret = new RetKit<String>(null);
		SysUser sysUser = sysUSerMapper.findUserById(String.valueOf(user.getId()));
		if (sysUser == null) {
			ret.setFail();
			ret.setMsg("用户不存在！");
			return ret;
		}
		
		try {
			int rows = sysUSerMapper.saveEditInfo(user);
			ret.setSuccess();
		} catch (Exception e) {
			// TODO: handle exception
			ret.setFail();
			ret.setMsg("修改用户信息异常！");
		}
		
		return ret;
	}

}
